﻿using Dapper;
using FRMS.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FRMS.Helper
{
    public class SqlHelper<T>
    {
        public static bool SqlExecute(string strsql)
        {
            /* try
             {*/
            using (var conn = DataFactory.GetSQLConnection())
            {
                int count = conn.Execute(strsql);

                if (count >= 0)
                {
                    return true;
                }
                return false;
            }
            /*}
            catch
            {
                return false;
            }*/
        }

        public static bool SqlSessionExecute(string strsql)
        {
            /* try
             {*/
            using (var conn = DataFactory.GetSQLConnection())
            {
                int count = conn.Execute(strsql);

                if (count > 0)
                {
                    return true;
                }
                return false;
            }
            /*}
            catch
            {
                return false;
            }*/
        }

        public static int SqlExecuteRows(string strsql)
        {
            /*try
            {*/
            using (var conn = DataFactory.GetSQLConnection())
            {

                int count = conn.Execute(strsql);

                return count;
            }
            /*}
            catch
            {
                return -1;
            }*/
        }

        public static List<T> SqlList(string strsql)
        {
            using (var conn = DataFactory.GetSQLConnection())
            {
                try
                {
                    CommFN<T> fn = new CommFN<T>();
                    return fn.GetListData(strsql);
                }
                catch (Exception ex)
                {
                    LogHelper.Log(ex.ToString());
                    return null;
                }
            }
        }

        public static int SqlScalar(string strsql)
        {
            int count = 0;
            /* try
             {*/
            using (var conn = DataFactory.GetSQLConnection())
            {
                count = conn.ExecuteScalar<int>(strsql);
            }
            /*}
            catch (Exception ex)
            {
                count = 0;
            }*/
            return count;
        }

        public static T SqlSingleData(string strsql)
        {
            try
            {

                CommFN<T> fn = new CommFN<T>();
                return fn.GetData(strsql);
            }
            catch (Exception e)
            {
                LogHelper.Log(e.ToString());
                return default(T);
            }
        }

        public static bool SqlDelete(string strsql)
        {
            /*try
            {*/
            using (var conn = DataFactory.GetSQLConnection())
            {

                int count = conn.Execute(strsql);

                if (count >= 0)
                {
                    return true;
                }
                return false;
            }
            /* }
             catch
             {
                 return false;
             }*/
        }

        public static string SqlScalarString(string strsql)
        {
            string str;
            /*try
            {*/
            using (var conn = DataFactory.GetSQLConnection())
            {
                str = conn.ExecuteScalar<string>(strsql);
            }
            /* }
             catch
             {
                 str = "";
             }*/
            return str;
        }

        public static List<T> SqlServerList(string strsql)
        {

            try
            {
                CommFN<T> fn = new CommFN<T>();
                return fn.GetSqlServerListData(strsql);
            }
            catch(Exception ex)
            {
                LogHelper.Log(ex.ToString());
                return null;
            }
        }

        public static bool SqlServerExecute(string strsql)
        {
            try
            {
                using (var conn = DataFactory.GetSQLServerConnection())
                {
                    int count = conn.Execute(strsql);

                    if (count >= 0)
                    {
                        return true;
                    }
                    return false;
                }
            }
            catch(Exception e)
            {
                LogHelper.Log(e.ToString());
                return false;
            }
        }

        public static int SqlServerScalar(string strsql)
        {
            int count = 0;
            try
            {
                using (var conn = DataFactory.GetSQLServerConnection())
                {
                    count = conn.ExecuteScalar<int>(strsql);
                }
            }
            catch (Exception ex)
            {
                LogHelper.Log(ex.ToString());
                count = 0;
            }
            return count;
        }

        public static List<T> OASqlList(string strsql)
        {

            try
            {
                CommFN<T> fn = new CommFN<T>();
                return fn.GetOASqlListData(strsql);
            }
            catch (Exception ex)
            {
                LogHelper.Log(ex.ToString());
                return null;
            }
        }

    }
}
